<template>
  <el-dialog
    v-el-drag-dialog
    title="地址"
    :visible.sync="visible"
    width="1024px"
    append-to-body
    destroy-on-close
  >
    <baidu-map
      v-if="visible"
      :scroll-wheel-zoom="true"
      class="bm-view"
      :center="center"
      :zoom="zoom"
    >
      <bm-map-type
        :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
        anchor="BMAP_ANCHOR_TOP_RIGHT"
      />
      <bm-marker
        v-if="marker"
        :position="marker"
        :dragging="true"
        animation="BMAP_ANIMATION_BOUNCE"
      />
    </baidu-map>
  </el-dialog>
</template>

<script>
  export default {
    name: 'csm-location',
    data() {
      return {
        visible: false,
        center: {},
        marker: null,
        zoom: 15
      }
    },
    methods: {
      open(temp) {
        return new Promise((resolve) => {
          this.resolve = resolve
          if (temp && temp.lon && temp.lat) {
            this.center.lng = temp.lon
            this.center.lat = temp.lat
            this.marker = { ...this.center }
            console.log('marker', this.marker)
            this.$nextTick(() => {
              this.visible = true
            })
          }
        })
      }
      // mapReady({ BMap, map }) {
      //   const _this = this
      //   // 获取自动定位方法
      //   this.geolocation = new BMap.Geolocation()
      //   // 获取逆解析方法实例
      //   this.myGeo = new BMap.Geocoder()
      //   if (this.center.lng && this.center.lat) {
      //     return
      //   }
      //   // 获取自动定位获取的坐标信息
      //   this.geolocation.getCurrentPosition(function(r) {
      //     console.log('对应id', r)
      //     if (r) {
      //       _this.center = {
      //         lng: r.point.lng,
      //         lat: r.point.lat
      //       }
      //       _this.point = {
      //         lng: r.point.lng,
      //         lat: r.point.lat
      //       }
      //       _this.pointToAddress(r.point)
      //     } else {
      //       const companyInfo = JSON.parse(sessionStorage.getItem('companyInfo'))
      //       console.log('companyInfo', companyInfo)
      //       if (companyInfo) {
      //         _this.center = companyInfo.city || '北京'
      //         _this.zoom = 12
      //       }
      //     }
      //     // _this.pointToAddress(r.point)
      //   }, { enabaleHighAccuracy: false })
      // }
    }
  }
</script>

<style lang="scss" scoped>
/deep/.el-dialog .el-dialog__body {
  padding: 0;
}
.bm-view {
  width: 100%;
  height: 500px;
}
</style>
